home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / WINGAMES / FGWTET11.ZIP / TETRIS.DOC < prev    next >
Encoding:
Text File  |  1995-11-02  |  10.9 KB  |  274 lines

  1.                       Fastgraph Tetris for Windows
  2.                       ----------------------------
  3.                              By Diana Gruber
  4.  
  5. I wrote this program for several reasons. First, I am trying to figure
  6. out how Delphi works. Don't laugh. Delphi is powerful and
  7. non-trivial. There are a lot of undocumented features. Even the
  8. documented ones are sometimes cryptic and non-obvious. I'm making
  9. slow progress.
  10.  
  11. Second, I wanted to beta-test Fastgraph for Windows. Somebody needs to
  12. keep an eye on Ted and make sure he is writing it right.
  13.  
  14. Third, I wanted to provide a demo program for our Fastgraph/Delphi
  15. users, so they can get an idea how to write Windows games. This is
  16. perhaps not the most brilliant Windows program ever written, but it's
  17. a start. Very likely, you will find better ways to solve some of the
  18. problems this program addresses. If so, please tell me.
  19.  
  20. Finally, I was asked to judge a contest sponsored by the x2.ftp.oulu
  21. group.  One of the contest categories is "best tetris". I thought
  22. tackling tetris programming myself would make me a better judge.
  23.  
  24. So here it is, my version of tetris for Windows. It took me about
  25. three weeks to write the program. My understanding of Windows
  26. programming and Delphi programming has improved considerably, and
  27. hopefully looking at this program will shed some light on the process
  28. for you too.
  29.  
  30. How to install tetris
  31. ---------------------
  32.  
  33. I didn't write an install program for tetris. I've been meaning to do
  34. that.  All you need to do to run tetris is copy all the files into a
  35. subdirectory.  Then you can run the program from the Windows program
  36. manager or from the Delphi IDE.
  37.  
  38. Files in the distribution
  39. -------------------------
  40.  
  41. TETICO.ICO  The tetris icon, for minimizing
  42. TETRIS.DCU  Compiled version of TETRIS.PAS
  43. TETRIS.DFM  Delphi project file
  44. TETRIS.PAS  Pascal source code
  45. TETRIS.SPR  Graphics in RLE format.
  46. WINTET.DPR  Source code for the Delphi project
  47. WINTET.EXE  Executable program
  48. WINTET.OPT  Delphi Options
  49. WINTET.RES  Compiled resource file
  50. EATING.WAV  Sound effect
  51. JAYLENO.WAV Sound effect
  52.  
  53. How to play tetris
  54. ------------------
  55.  
  56. Launch the game from Delphi, or from the Windows program manager.
  57. When the play field appears, try to line up blocks in such a way as
  58. to fill a horizontal row. When an entire row is filled, it will be
  59. munched by langoliers. Then all the rows above it will move down a
  60. row. Keep doing this until you get to the top of the playfield. At
  61. this point, you lose. Play the game again. Keep doing it. This is
  62. entertaining. Honest, it is.
  63.  
  64. About the source code
  65. ---------------------
  66.  
  67. The source code is written in Borland's Object Pascal, which is what
  68. Delphi really is, under that fancy shell. It is linked with Fastgraph
  69. for Windows, final beta version.
  70.  
  71. A form is created, and given a size calculated to be twice the size
  72. of the virtual buffer. More about that in a minute. The form is
  73. called TForm1 and is defined as follows:
  74.  
  75.   TForm1 = class(TForm)
  76.     Panel1: TPanel;
  77.     Button1: TButton;
  78.     Timer1: TTimer;
  79.     Header1: THeader;
  80.     procedure AppOnActivate(Sender: Tobject);
  81.     procedure FormCreate(Sender: TObject);
  82.     procedure FormDestroy(Sender: TObject);
  83.     procedure FormPaint(Sender: TObject);
  84.     procedure FormResize(Sender: TObject);
  85.     procedure drop_block(Sender: TObject);
  86.     procedure pause(Sender: TObject);
  87.   end;
  88.  
  89. Four components are added to a form. These are:
  90.  
  91.    Panel1: a panel component, completely hidden by the button component.
  92.    This component keeps the other components from constantly changing
  93.    focus. Every time you press an arrow key, the focus would switch from
  94.    the header to the button and back again, causing flickering. The
  95.    panel fixes that.
  96.  
  97.    Button1: a button component, to allow the user to pause and resume the
  98.    game.
  99.  
  100.    Timer1: the timer component, which triggers each frame of animation.
  101.  
  102.    Header1: the header component, which shows the score.
  103.  
  104. In addition, several functions are added to the form. Some of these
  105. are added automatically through the object inspector. Others must be
  106. created manually.  The automatic ones have the letters "Form" as a
  107. prefix. Here are descriptions of what these procedures do:
  108.  
  109.    procedure AppOnActivate(Sender: Tobject);
  110.  
  111. This is a manually created procedure that executes when control
  112. switches from another Windows application to tetris, or in other
  113. words, when tetris becomes the top-level window. It is analogous to
  114. the WM_SETFOCUS Windows message handler. We use it in tetris to
  115. realize the application's logical palette, in case the other
  116. application used a different palette.
  117.  
  118.    procedure FormCreate(Sender: TObject);
  119.  
  120. This procedure is called one time when the program begins. It is a
  121. good place to set up the device context, logical palette, and virtual
  122. buffers. We also enable the AppOnActivate procedure. It is analogous
  123. to the WM_CREATE Windows message handler.
  124.  
  125.    procedure FormDestroy(Sender: TObject);
  126.  
  127. This procedure is called one time when the program exits. It releases
  128. the virtual buffers and other resources. It is analogous to the
  129. WM_DESTROY Windows message handler.
  130.  
  131.    procedure FormPaint(Sender: TObject);
  132.  
  133. This procedure is called whenever any portion of the form needs to be
  134. redrawn.  In Delphi, the invalidate procedure (called in
  135. AppOnActivate) will trigger this. Other things can trigger this
  136. procedure too, such as when another Window overlaps part of the form,
  137. or when the form size changes. It is analogous to the WM_PAINT
  138. Windows message handler.
  139.  
  140.    procedure FormResize(Sender: TObject);
  141.  
  142. This procedure is called when the size of the form changes, and also
  143. as part of the form creation. It simply records the form dimensions.
  144. It is analogous to the WM_SIZE Windows message handler.
  145.  
  146.    procedure drop_block(Sender: TObject);
  147.  
  148. This function was manually created to handle the tetris animation. It
  149. is triggered at regular intervals by the timer component. Each time
  150. this procedure is called, it is considered a "frame of animation".
  151. Usually it involves a block moving downward by several pictures, and
  152. the virtual buffer is blitted to the form.
  153.  
  154.    procedure pause(Sender: TObject);
  155.  
  156. This function was manually created to allow the user to pause the
  157. game, for example when the telephone rings, or when you want to run
  158. another application while running tetris. It is triggered by the
  159. button component.
  160.  
  161. Several other procedures were added as well. These are called by the
  162. drop_block procedure, and handle some moving and displaying the
  163. blocks, and playing the game.
  164.  
  165. Size of the frame, virtual buffers, and screen
  166. ----------------------------------------------
  167.  
  168. The virtual buffer must be scaled to fit in the window. The frame
  169. defines the size of the window. If the size difference is a factor of
  170. two, the scaling function does less work and is therefore faster. Of
  171. course, the user can scale the window, and the program will still
  172. work. But for efficiency, the program starts with a nice even
  173. multiple of the window size and the virtual buffer size, as follows:
  174.  
  175.   Virtual buffer size:             240x200
  176.   Frame size:                      480x400
  177.   Play area:                        80x160
  178.  
  179. The entire virtual buffer is blitted to the frame during a Windows
  180. paint event. During a timer event, however, when the game is
  181. animating, it is not necessary to blit the entire buffer. Only the
  182. play area is blitted. This is more efficient. I blitted the entire
  183. height of the virtual buffer (200 rows) rather than calculating the
  184. length and scaling. Scaling factors are unpredictable as windows are
  185. resized. A partial blit could cause an undesirable stretching or
  186. jaggy effect.
  187.  
  188. Palette
  189. -------
  190.  
  191. I grabbed the palette values from a PCX file and put them in an array.
  192. Then I called fg_mapdacs() to translate the RGB components from the
  193. 0-63 range used in DOS to the 0-255 range used in Windows. The
  194. logical palette is created using fg_logpal(), passing it the 236
  195. colors in the palette. This is set up in the FormCreate procedure.
  196. The palette is realized with a call to fg_realize() in FormCreate and
  197. also in AppOnActivate.
  198.  
  199. Important: remember you can only use 236 colors out of 256 in Windows.
  200. The first 10 and last 10 are reserved for Windows. When designing
  201. bitmaps, etc., try to use the colors not reserved by Windows. That
  202. way, you will have more control over them.
  203.  
  204. Animation
  205. ---------
  206.  
  207. That little langolier critter is cute, isn't he? He's a simple 256
  208. color bitmap in two frames. He is blitted to the virtual buffer using
  209. fg_drwimage().  The remnants of blocks he leaves behind as he munches
  210. are drawn with single-color bitmaps using fg_drawmap(). The color is
  211. determined by looking at the block before it is erased, with
  212. fg_getpixel(). Then the color is set with fg_setcolor(). This is
  213. simple, and it works well.
  214.  
  215. Double-buffering is achieved by creating another virtual buffer, just
  216. slightly larger than a row of blocks. This holds the clean copy of
  217. the blocks, so as the langoliers munch they can be moved without
  218. leaving remnants.
  219.  
  220. About Fastgraph for Windows
  221. ---------------------------
  222.  
  223. Fastgraph for Windows allows you to build great graphics applications
  224. for Windows the same way you do for DOS. Powerful, fast low-level
  225. graphics routines interface with your choice of WinG, DirectDraw or
  226. the Windows API and enable you to create fast, smooth animation under
  227. Windows 3.1 and Windows 95.  Both 16- and 32-bit libraries support
  228. most popular C/C++ compilers, Borland Pascal, and Delphi. Port your
  229. Fastgraph DOS code to Windows with just a few simple modifications.
  230. Graphics techniques are extensively documented in a comprehensive
  231. 600-page manual. Virtual buffers, bitmaps, file formats, color
  232. palette management, graphics primitives, scaling, blitting, animation,
  233. double and triple buffering, and printing are all supported in a
  234. simple, intuitive, well-documented manner. Writing games for Windows
  235. doesn't get much easier than this! Fastgraph for Windows is only
  236. $249, with no royalties.
  237.  
  238. Contact information for Fastgraph products
  239. ------------------------------------------
  240.  
  241. Ted Gruber Software
  242. P.O. Box 13408
  243. Las Vegas, NV 89112
  244. (702) 735-1980 (voice)
  245. (702) 735-4603 (fax)
  246. (702) 796-7134 (bbs)
  247.  
  248. orders only: 1-800-410-0192 (The Coriolis Group)
  249.  
  250. email:
  251. Fastgraph@AOL.COM
  252. 72000.1642@compuserve.com
  253.  
  254. Our ftp site for demos, shareware versions, updates, and lots of games
  255. written with Fastgraph is:
  256.  
  257. ftp.accessnv.com
  258.   or
  259. http://www.accessnv.com/fastgraph/
  260.  
  261. Please contact us if you want more information about Fastgraph,
  262. including the current brochure, demo disk and order form.
  263.  
  264. Other Credits
  265. -------------
  266.  
  267. Big thanks to Tim Feldman for explaining how to play WAV files in his
  268. article in the Aug/Sep 1995 issue of PC Techniques. the sndPlaySound
  269. function is a great example of a useful but completely undocumented
  270. feature. Way to go, Borland! (Not!)
  271.  
  272. Thanks, Tim! Also thanks to Tom Repstad and Steve Blackwood for
  273. programming help.
  274.